/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.basic.park.service.impl;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.basic.common.constant.enums.ParkBasicSysDictEnum;
import com.hopes.bizz.basic.gis.vo.ForbiddenControlVo;
import com.hopes.bizz.basic.park.dto.BaseParkForbiddenControlExcelDTO;
import com.hopes.bizz.basic.park.entity.BaseParkForbiddenControlEntity;
import com.hopes.bizz.basic.park.mapper.BaseParkForbiddenControlMapper;
import com.hopes.bizz.basic.park.service.BaseParkForbiddenControlService;
import com.hopes.bizz.basic.park.vo.BaseParkForbiddenControlExcelVO;
import com.hopes.bizz.common.core.validate.ValidatorUtils;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 禁限控目录
 *
 * @author pig
 * @date 2023-10-23 14:45:34
 */
@Service
public class BaseParkForbiddenControlServiceImpl extends ServiceImpl<BaseParkForbiddenControlMapper, BaseParkForbiddenControlEntity> implements BaseParkForbiddenControlService {

    @Override
    public ValidateImportBO<BaseParkForbiddenControlExcelDTO> validateImport(List<BaseParkForbiddenControlExcelDTO> excelDTOList) {
        ValidateImportBO<BaseParkForbiddenControlExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<BaseParkForbiddenControlExcelDTO> saveList = new ArrayList<>();
        int successNum = 0;
        int failedNum = 0;

        // 做校验
        for (BaseParkForbiddenControlExcelDTO excelDTO : excelDTOList) {
            String validateRes = ValidatorUtils.validateEntity(excelDTO);
            if (StrUtil.isEmpty(validateRes)) {
                successNum++;
                saveList.add(excelDTO);
            } else {
                failedNum++;
                errorMessageList.add(new ErrorMessage(excelDTO.getLineNum(), new HashSet<String>() {{
                    add(validateRes);
                }}));
            }

        }
        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public BaseParkForbiddenControlEntity importDtoToEntity(BaseParkForbiddenControlExcelDTO excelDto) {
        BaseParkForbiddenControlEntity entity = new BaseParkForbiddenControlEntity();
        String controlType = excelDto.getControlType();
        String tradeType = excelDto.getTradeType();
        excelDto.setControlType(DictResolver.getItemValueByLabel(ParkBasicSysDictEnum.control_type, controlType));
        excelDto.setTradeType(DictResolver.getItemValueByLabel(ParkBasicSysDictEnum.trade_type, tradeType));
        BeanUtils.copyProperties(excelDto, entity);
        return entity;
    }

    @Override
    public BaseParkForbiddenControlExcelVO exportEntityToVo(BaseParkForbiddenControlEntity entity) {
        BaseParkForbiddenControlExcelVO excelVO = new BaseParkForbiddenControlExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        return excelVO;
    }

    @Override
    public ForbiddenControlVo queryForbiddenControl() {
        ForbiddenControlVo forbiddenControlVo = new ForbiddenControlVo();
        Map<String, Long> forbidMap = baseMapper.selectList(Wrappers.lambdaQuery(BaseParkForbiddenControlEntity.class))
                .stream().collect(Collectors.groupingBy(BaseParkForbiddenControlEntity::getControlType, Collectors.counting()));
        JSONArray pieData = new JSONArray(forbidMap.size());
        ArrayList<String> name = new ArrayList<>();
        ArrayList<Long> count = new ArrayList<>();
        JSONObject barData = new JSONObject();
        barData.put("name",name);
        barData.put("count",count);
        forbidMap.forEach((k,v)->{
            JSONObject data = new JSONObject();
            data.put("name",DictResolver.getItemLabelByValue(ParkBasicSysDictEnum.control_type,k));
            data.put("value",v);
            pieData.add(data);
            name.add(DictResolver.getItemLabelByValue(ParkBasicSysDictEnum.control_type,k));
            count.add(v);
        });
        forbiddenControlVo.setPieData(pieData);
        forbiddenControlVo.setBarData(barData);
        return forbiddenControlVo;
    }
}
